package com.gopher.gopherbeasts;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.gopher.gopherbeasts.Controls.Control;

class Input {
	public int key;
	public boolean down;

	public Input(int key, boolean down) {
		this.key = key;
		this.down = down;
	}
}

public class InputHandler implements InputProcessor {

	private Map<Integer, Control> map = new HashMap<Integer, Control>();
	private static Controls controls;

	private Queue<Input> keyq = new LinkedList<Input>();

	public static Controls getControls() {
		return controls;
	}

	public InputHandler() {
		controls = new Controls();

		map.put(Keys.SPACE, controls.jump);
		map.put(Keys.UP, controls.jump);
		map.put(Keys.W, controls.jump);

		map.put(Keys.LEFT, controls.left);
		map.put(Keys.A, controls.left);
		// figure out android buttons

		map.put(Keys.RIGHT, controls.right);
		map.put(Keys.D, controls.right);
		map.put(Keys.E, controls.right); // Dvorak

	}

	public void update() {
		while (!keyq.isEmpty()) {
			Input input = keyq.remove();
			Control c = map.get(input.key);
			if (c != null) {
				c.nextState = input.down;
			}
		}

		controls.update();
	}

	@Override
	public boolean keyDown(int keycode) {
		keyq.add(new Input(keycode, true));
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		keyq.add(new Input(keycode, false));
		return false;
	}

	// this works for both touch events and screen click events, cool!
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		System.out.println(screenX + "x " + screenY + "y");
		return false;
	}

	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		return false;
	}

	public boolean touchDragged(int screenX, int screenY, int pointer) {
		return false;
	}

	public boolean mouseMoved(int screenX, int screenY) {
		return false;
	}

	public boolean scrolled(int amount) {
		return false;
	}

	// prob not gonna use this method
	public boolean keyTyped(char character) {
		return false;
	}
}
